import axios from '@/api'
import { pick } from 'lodash-es'

export interface IFile {
  iid: string;
  usr_id: string;
  s_a_id: string;
  i_time: string;
  f_time: string;
  desc: string;
  file_type: string;
  file_type_desc: string;
  statu: string;
  statu_desc: string;
  error_msg: string;
}

class FileServices {
  constructor() {}


  /**
   * 获取文件列表
   *
   * @param {(Partial<Pick<IFile, 'file_type' | 'statu' | 'desc'>> & Api.PageOptions)} data
   * @returns
   * @memberof FileServices
   */
  getFiles(data: Partial<Pick<IFile, 'file_type' | 'statu' | 'desc'>> & Api.PageOptions & { i_time_sort?: string, f_time_sort?: string; }) {
    return axios.post<unknown, Api.ResponseListOptions<IFile[]>>('/btbusiness/file-history/get-list', data)
  }


  /**
   * 上传文件
   *
   * @param {(Partial<Pick<IFile, 'iid' | 'file_type' | 'desc'>>} data
   * @returns
   * @memberof FileServices
   */
  uploadFile(data: Partial<IFile>) {
    const formData = new FormData()
    for(let i in data) {
      // @ts-ignore
      formData.append(i, data[i])
    }
    return axios.post<unknown, Api.ResponseOptions>('/btbusiness/file-history/edit-file', formData)
  }


  /**
   * 删除上传记录（本人操作）
   *
   * @param {Pick<IFile, 'iid'>} data
   * @returns
   * @memberof FileServices
   */
  delete(data: Pick<IFile, 'iid'>) {
    return axios.post<unknown, Api.ResponseOptions>('/btbusiness/file-history/del', data)
  }


  /**
   * 文件下载
   *
   * @param {Pick<IFile, 'iid'>} data
   * @returns
   * @memberof FileServices
   */
  download(data: Pick<IFile, 'iid'>) {
    return axios.post<unknown, Api.ResponseOptions>('/btbusiness/file-history/get-file', data, {responseType: 'blob', customHandler: true})
  }

  /**
   * 文件入库
   *
   * @param {Pick<IFile, 'iid'>} data
   * @returns
   * @memberof FileServices
   */
  handle(data: Pick<IFile, 'iid'>) {
    return axios.post<unknown, Api.ResponseOptions>('/btbusiness/upload/handle', data)
  }
}

export const fileServices = new FileServices()

export default fileServices